package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.ShiftClassRecordDetail;
import com.yuyou.fn.educational.mapper.ShiftClassRecordDetailMapper;
import com.yuyou.fn.educational.service.IShiftClassRecordDetailService;
import com.yuyou.fn.educational.vo.ShiftClassRecordDetailQueryVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *
 * ShiftClassRecordDetail 表数据服务层接口实现类
 *
 */
@Service
public class ShiftClassRecordDetailServiceImpl extends SuperServiceImpl<ShiftClassRecordDetailMapper, ShiftClassRecordDetail,String> implements IShiftClassRecordDetailService {


    @Override
    public Page<ShiftClassRecordDetail> findShiftClassRecordDetailByQuery(ShiftClassRecordDetailQueryVo shiftClassRecordDetailQueryVo, Integer pageNo, Integer pageSize) {

        if(shiftClassRecordDetailQueryVo.getEndDate() != null ){
            shiftClassRecordDetailQueryVo.setEndDate(DateUtils.addDays(shiftClassRecordDetailQueryVo.getEndDate(),1 ));
        }

        Example<ShiftClassRecordDetail> example = ShiftClassRecordDetail.createExample();
        example.orderBy("createTime").desc() ;
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillCondition(criteria,shiftClassRecordDetailQueryVo);
        return findPageByExample(example,pageNo,pageSize);
    }

    @Override
    public SXSSFWorkbook exportShiftClassRecordDetailByQuery(ShiftClassRecordDetailQueryVo shiftClassRecordDetailQueryVo) {
        Page<ShiftClassRecordDetail> page=this.findShiftClassRecordDetailByQuery(shiftClassRecordDetailQueryVo,1,50000);
        return exportShiftClassRecordDetailByQuery(page.getRecords());
    }

    public static SXSSFWorkbook exportShiftClassRecordDetailByQuery(List<ShiftClassRecordDetail> shiftClassRecordDetailList) {

        SXSSFWorkbook excel = new SXSSFWorkbook();
        SXSSFSheet sheet = excel.createSheet("调课记录");
        SXSSFRow firstRow = sheet.createRow(0);
        String[] titles = new String[]{"学生姓名","操作人","调课操作时间","原来班级名称","原班讲次","原来任课教师","转入校区",	"新班级名称","新班级讲次","上课时间","学生电话"};
        for (int i = 0; i < titles.length; i++) {
            firstRow.createCell(i).setCellValue(titles[i]);
        }
        int rowNum=1;
        for (ShiftClassRecordDetail  shiftClassRecordDetail:shiftClassRecordDetailList){
            int cellNum = 0;
            SXSSFRow row = sheet.createRow(rowNum++);

            SXSSFCell cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(shiftClassRecordDetail.getStudentName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(shiftClassRecordDetail.getOperator()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(shiftClassRecordDetail.getCreateTime()==null?"": Utils.formatDate(shiftClassRecordDetail.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(shiftClassRecordDetail.getFromClassName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(shiftClassRecordDetail.getFromLectureNo()==null?"":shiftClassRecordDetail.getFromLectureNo().toString());

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(shiftClassRecordDetail.getFromTeacherName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(shiftClassRecordDetail.getCampusName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(shiftClassRecordDetail.getToClassName()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(shiftClassRecordDetail.getToLectureNo()==null?"":shiftClassRecordDetail.getToLectureNo().toString());

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(shiftClassRecordDetail.getToStudyingTime()));

            cell = row.createCell(cellNum++);
            cell.setCellValue(StringUtils.defaultString(shiftClassRecordDetail.getStudentPhoneNo()));



        }
        return excel;
    }


}